Web Services

Add a Web Service to Your Application

Code Customization
Add a Web Service to Your Application
Description:

This example shows how to create a web service that adds, deletes, and updates records from a database table.

The database table used in this example is the Shippers table in the Microsoft SQL Server Northwind database.

Procedure:

Step 1:  Create an application in Iron Speed Designer using the Application Wizard. Be sure to use the table(s) for which you wish to add a web service. This example uses the Shippers table in Northwind.

Step 2:  Build the application.

Step 3:  Add a reference to System.Web.Services.dll in the application's CompileApplication.rsp file. This is optional if you are using Visual Studio .NET to compile the application, but required if you are using csc.exe to compile the application

.\MyApp\CompileApplication.rsp
        

Step 4:  Open application in Visual Studio .NET.

Open Visual Studio and click on Open -> Web Site and point to your application's directory.

Step 5:  Add a Web Service to your application. (Add New Item, Web Service).

.\MyApp\MyWebServiceDirectory\MyWebServiceForTableAccess.asmx
        

Step 6:  Switch the Web Service to code view, and add functions similar to the following to the CSharp.NET class for the Web Service (MyWebServiceForTableAccess.asmx.cs):

// Web service function to Add a Shippers record
// Input: company name, phone number
// Output: id of created record
[WebMethod()]
public int AddShippersRecord(string companyName, string phone)
{
    // Set up object for the primary key of new record to be inserted.
    int createdShipperId = -1;

    // Create and obtain a transaction object.
    BaseClasses.Data.SqlProvider.SqlTransaction t;
    t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess");
    try
    {
        // Create a new record and populate it.
        ShippersRecord shipperRecord = new ShippersRecord();
        shipperRecord.SetCompanyNameFieldValue(companyName);
        shipperRecord.SetPhoneFieldValue(phone);

        // Save the new record.
        shipperRecord.Save();

        // Get the primary key of the new record inserted.
        BaseClasses.Data.KeyValue primaryKey;
        primaryKey = shipperRecord.GetID();
        createdShipperId = System.Convert.ToInt32(primaryKey.ColumnValueByName("ShipperID"));

        // Commit the transaction.
        t.Commit();
    }
    catch
    {
        // On exception, rollback the transaction.
        t.RollBack();
    }
    finally
    {
        // Finally release the transaction object.
        t.Release(true);
    }
    return createdShipperId;
}

// Web service function to Delete a Shippers record
// Input: shipper id
// Output: boolean indicating success/failure to delete record
[WebMethod()]
public bool DeleteShippersRecord(int shipperId)
{
    bool isSuccessfullyDeleted = false;

    // Create and obtain a transaction object.
    BaseClasses.Data.SqlProvider.SqlTransaction t;
    t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess");
    try
    {
        // Set the Shippers table data access class.
        ShippersTable shipperAccess = ShippersRecord.TableUtils;

        // Set up primary key for the record to delete.
        BaseClasses.Data.KeyValue primaryKey = new BaseClasses.Data.KeyValue();
        primaryKey.AddElement(shipperAccess.ShipperIDColumn.InternalName, shipperId.ToString());

        // Delete the record using the above primary key.
        ShippersTable.DeleteRecord(primaryKey);

        // Commit the transaction.
        t.Commit();

        // Flag a successful delete.
        isSuccessfullyDeleted = true;
    }
    catch (Exception e)
    {
        // Rollback the transaction.
        t.RollBack();
    }
    finally
    {
        // Release the transaction.
        t.Release(true);
    }
    return isSuccessfullyDeleted;
}

// Web service function to Update a Shippers record
// Input: shipper id
// Output: boolean indicating success/failure to update record
[WebMethod()]
public bool UpdateShippersRecord(int shipperId, string companyName, string phoneNumber)
{
    bool isSuccessfullyUpdated = false;

    // Create and obtain a transaction object.
    BaseClasses.Data.SqlProvider.SqlTransaction t;
    t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess");
    try
    {
        // Set the Shippers table data access class.
        ShippersTable shipperAccess = ShippersRecord.TableUtils;

        // Set up primary key for the record to update.
        BaseClasses.Data.KeyValue primaryKey = new BaseClasses.Data.KeyValue();
        primaryKey.AddElement(shipperAccess.ShipperIDColumn.InternalName, shipperId.ToString());

        // Update the record using the above primary key.
        ShippersRecord shipperRec = ShippersTable.GetRecord(primaryKey, true);
        shipperRec.SetCompanyNameFieldValue(companyName);
        shipperRec.SetPhoneFieldValue(phoneNumber);

        // Save the record.
        shipperRec.Save();

        // Commit the transaction.
        t.Commit();

        // Flag a successful update.
        isSuccessfullyUpdated = true;
    }
    catch (Exception e)
    {
        // Rollback the transaction.
        t.RollBack();
    }
    finally
    {
        // Release the transaction.
        t.Release(true);
    }
    return isSuccessfullyUpdated;
}
    

Step 7:   Add the following statement at the top of your webservices.asmx.cs file

using <Application Name>.Business;

Replace <Application Name> with your application name.

Step 8:  Compile the application in Visual Studio .NET.

Step 9:  The web service now has three supported operations: Add, Delete, and Update.

It can be accessed via http://localhost/<App Name>/<Folder>/<Web Service Name>

Disclaimer:
Customizations included in this wizard are provided as a sample to demonstrate a feature, and may work only in specific situations. The Iron Speed Designer support team cannot assist in the resolution of problems which may occur as a result of customizing your application or modifying a customization to fit a particular purpose.

Terms of Service Privacy Statement